package com.jxs.android.weiget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

import com.jxs.android.ges.R;

public class ClickAbleText extends TextView {
	private boolean isClickable;
	private int linkedColor;
	
	private Paint mPaint;
	
	public ClickAbleText(Context context){
		this(context, null);
	}
	
	public ClickAbleText(Context context, AttributeSet attrs){
		this(context, attrs, 0);
	}
	
	public ClickAbleText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		int grayColor = context.getResources().getColor(R.color.LightGray);
		isClickable = true;
		linkedColor = grayColor;
		if(attrs != null){
			TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.clickAbleText);
			isClickable = a.getBoolean(R.styleable.clickAbleText_isClick, true);
			linkedColor = a.getColor(R.styleable.clickAbleText_linkedLineColor, grayColor);
		}
		setupView();
	}
	
	void setupView(){
		mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setColor(linkedColor);
		mPaint.setAntiAlias(true);
	}
	
	void setClickListener(OnClickListener l){
		if(isClickable){
			setOnClickListener(l);
		}
	}
	public boolean isClickAble(){
		return isClickable;
	}
	
	public void setClickAble(boolean clickAble){
		isClickable = clickAble;
	}
	
	public void setLinkedColor(int linkedColor){
		this.linkedColor = linkedColor;
		mPaint.setColor(linkedColor);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		if(isClickAble()){
			int pointY = getBaseline() + 6;
			float width = getTextSize() * getText().length();
			int offSetX = (int) ((getWidth() - getPaddingLeft() - getPaddingRight() - width) / 2);
			canvas.drawLine(offSetX + getPaddingLeft(), pointY, offSetX + getPaddingLeft() + width, pointY, mPaint);
		}
		
	}
	
	
}